﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Conference.Logic.Base;

namespace Conference.Logic.Proposal
{
    /// <summary>
    /// Class that represents all proposals
    /// </summary>
    public class Proposals
    {
        #region Attributes
        private List<ItemProposal> list;
        #endregion

        #region Properties
        /// <summary>
        /// Return the proposal list. Read only property.
        /// </summary>
        public List<ItemProposal> List
        {
            get { return this.list; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Create a new proposal object
        /// </summary>
        public Proposals()
        {
            try
            {
                list = new List<ItemProposal>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Create a new proposal object and can fill with sample input
        /// </summary>
        /// <param name="buildSample">If true, fill the proposal according with test input</param>
        public Proposals(bool buildSample)
        {
            try
            {
                list = new List<ItemProposal>();
                if (buildSample)
                    this.FillSample();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Methods
        /// <summary>
        /// Fill proposal like test input
        /// </summary>
        public void FillSample()
        {
            try
            {
                this.Add("Writing Fast Tests Against Enterprise Rails", ItemType.Default, 60);
                this.Add("Overdoing it in Python", ItemType.Default, 45);
                this.Add("Lua for the Masses", ItemType.Default, 30);
                this.Add("Ruby Errors from Mismatched Gem Versions", ItemType.Default, 45);
                this.Add("Common Ruby Errors", ItemType.Default, 45);
                this.Add("Rails for Python Developers", ItemType.Lightning);
                this.Add("Communicating Over Distance", ItemType.Default, 60);
                this.Add("Accounting-Driven Development", ItemType.Default, 45);
                this.Add("Woah", ItemType.Default, 30);
                this.Add("Sit Down and Write", ItemType.Default, 30);
                this.Add("Pair Programming vs Noise", ItemType.Default, 45);
                this.Add("Rails Magic", ItemType.Default, 60);
                this.Add("Ruby on Rails: Why We Should Move On", ItemType.Default, 60);
                this.Add("Clojure Ate Scala (on my project)", ItemType.Default, 45);
                this.Add("Programming in the Boondocks of Seattle", ItemType.Default, 30);
                this.Add("Ruby vs. Clojure for Back-End Development", ItemType.Default, 30);
                this.Add("Ruby on Rails Legacy App Maintenance", ItemType.Default, 60);
                this.Add("A World Without HackerNews", ItemType.Default, 30);
                this.Add("User Interface CSS in Rails Apps", ItemType.Default, 30);
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        /// <summary>
        /// Get a list with all pending proposals
        /// </summary>
        /// <returns>List of ItemProposal object</returns>
        public List<ItemProposal> GetPendings()
        {
            try
            {
                return this.List.Where(i => !i.IsScheduled).ToList();
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        /// <summary>
        /// Add a new proposal item
        /// </summary>
        /// <param name="description">Name of the event</param>
        /// <param name="type">Type of event: Lightning or Default </param>
        /// <param name="minutes">Duration of the event</param>
        /// <returns>Success or Fail</returns>
        public bool Add(string description, ItemType type, int? minutes = null)
        {
            try
            {
                TimeSpan duration;
                
                if (type == ItemType.Lightning)
                    duration = Parameter.Default.LenghtLightning;
                else if (type == ItemType.Default)
                {
                    if (minutes.HasValue)
                        duration = new TimeSpan(0, minutes.Value, 0);
                    else
                        throw new Exception ("You must specify a value for minutes!");
                }
                else
                    throw new Exception ("Proposal type is unavailable!");

                this.list.Add(new ItemProposal(description, type, duration));

                return true;
            }
            catch (Exception ex)
            {           
                throw ex;
            }
        }

        /// <summary>
        /// Check if the proposal list is valid, e.g. contain itens
        /// </summary>
        /// <returns></returns>
        public bool IsValid()
        {
            try
            {
                return ((this.List != null) && this.List.Count > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if there is any proposal item hasn't been schedule yet
        /// </summary>
        /// <returns></returns>
        public bool HasPendingItems()
        {
            return (this.list.Where(i => !i.IsScheduled).Count() > 0);
        }
        #endregion
    }
}
